package com.hl.web.fore;

import com.hl.pojo.Article;
import com.hl.service.ArticleService;
import com.hl.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    public ArticleService articleService;
    @RequestMapping("/add")
    public ResultEntity add(@RequestBody Article article){
        article.setCreateTime((new SimpleDateFormat("yyyy-MM-dd HH-mm-ss")).format(new Date()));
        articleService.add(article);
        return ResultEntity.ok();
    }
    @GetMapping("/sosuo")
    public ResultEntity sosuo(String name){
        List<Article> list = articleService.findAll();
        for(int i=0;i<list.size();i++){
            boolean pd = false;
            if(list.get(i).getArticleName().contains(name))pd=true;
            if(!pd){
                list.remove(i);
                i--;
            }
        }
        return ResultEntity.ok(list);
    }
    @RequestMapping("/del")
    public ResultEntity del(Integer id){
        articleService.del(id);
        return ResultEntity.ok("删除成功！");
    }
    @RequestMapping("/findAll")
    public ResultEntity findAll(){
        return ResultEntity.ok(articleService.findAll());
    }
    @RequestMapping("/findByName")
    public ResultEntity findByName(Article article){
        return ResultEntity.ok(articleService.findByName(article));
    }
    @RequestMapping("/findByCategory")
    public ResultEntity findByCategory(Integer categoryId){
        return ResultEntity.ok(articleService.findByCategory(categoryId));
    }
    @RequestMapping("/findByConsultantId")
    public ResultEntity findByConsultantId(Integer consultantId){

        return ResultEntity.ok(articleService.findByConsultantId(consultantId));
    }
    @PostMapping("/update")
    public ResultEntity update(@RequestBody Article article) {
        articleService.update(article);
        return ResultEntity.ok("修改成功！");
    }
}
